//
//  OrderSeleectCity.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/6/15.
//

import UIKit

protocol OrderSeleectCityDelegate:class {
    func selectCityConfirmDelegateIsSender(isSender:String)
    func selectCityCancelDelegate()
}

class OrderSeleectCity: UIView, UITableViewDelegate, UITableViewDataSource/*, UITableViewDelegate, UITableViewDataSource */{
    weak var delegate:OrderSeleectCityDelegate?
    var cityAry:Array<Any>!//总城市数据源
    var isSender:String!//是否是寄件人筛选
    var orderStatus:String!//订单类型
    /*
     name = "\U963f\U5f04";
     regionId = 110100;
     regionIdCount = 8;
     */
    //总区域数据源
    var areaAry:Array<Any>!
    var isArea:Bool!//区分是否选择到了区域界面,
    var selectCityCount:String!//城市选中的第几个
    var selectAreaCount:String!//区域选中的第几个
    var isSelect:Bool = false
    
    var tempSelectCityCount:String!//城市选中的第几个 局部用
    var tempSelectAreaCount:String!//区域选中的第几个 局部用
    var tempIsSelect:Bool!//是否选中 局部用

    override init(frame: CGRect) {
        super.init(frame: frame)
        tempSelectCityCount = selectCityCount
        tempSelectAreaCount = selectAreaCount
        tempIsSelect = isSelect
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setUI() {
        self.addSubview(self.mainView)
        self.mainView.addSubview(self.cencelBtn)
        self.mainView.addSubview(self.titleLab)
        self.mainView.addSubview(self.resetBtn)
        self.mainView.addSubview(self.confirmBtn)
        self.mainView.addSubview(self.cityTableView)

        self.mainView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0)
            make.right.equalTo(self.snp.right).offset(0)
            make.bottom.equalTo(self.snp.bottom).offset(0)
            make.height.equalTo(Constants.ratioIntervalHeighrt * 517 + Constants.xSafeBottomHeight)
        }
        self.cencelBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.width.equalTo(40)
            make.top.equalTo(self.mainView.snp.top).offset(15)
            make.right.equalTo(self.mainView.snp.right).offset(-15)
        }
        self.titleLab.snp.makeConstraints { (make) in
            make.top.equalTo(self.mainView.snp.top).offset(15)
            make.centerX.equalTo(self.mainView.snp.centerX).offset(0)
            make.height.equalTo(22.5)
        }
        self.resetBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.width.equalTo(Constants.ratioInterval * 130)
            make.left.equalTo(self.snp.left).offset(20)
            make.bottom.equalTo(self.mainView.snp.bottom).offset(-(8 + Constants.xSafeBottomHeight))
        }
        self.confirmBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.width.equalTo(Constants.ratioInterval * 190)
            make.right.equalTo(self.snp.right).offset(-20)
            make.bottom.equalTo(self.mainView.snp.bottom).offset(-(8+Constants.xSafeBottomHeight))
        }
        self.cityTableView.snp.makeConstraints { (make) in
            make.top.equalTo(self.mainView.snp.top).offset(95)
            make.left.equalTo(self.snp.left).offset(20)
            make.right.equalTo(self.snp.right).offset(-20)
            make.bottom.equalTo(self.snp.bottom).offset(-20)
        }
    }
    
    @objc func clickCancelBtn() {
        tempSelectCityCount = ""
        tempSelectAreaCount = ""
        self.cityBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect), for: .normal)
        self.cityBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
        self.cityBtn.snp.updateConstraints { (make) in
           let width =  ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect).getWidth(UIFont.boldSystemFont(ofSize: 16))
            make.width.equalTo(width + 5)
        }
        self.areaBtn.setTitle("", for: .normal)
        isArea = false
        self.cityTableView.reloadData()
        self.delegate?.selectCityCancelDelegate()
    }
    
    @objc func clickBtn(sender:UIButton) {
        if sender.tag == 100 {
            self.cityBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect), for: .normal)
            self.cityBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
            self.cityBtn.snp.updateConstraints { (make) in
                let width =  ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect).getWidth(UIFont.boldSystemFont(ofSize: 16))
                 make.width.equalTo(width + 5)
            }
            self.cityLineView.isHidden = false
            self.areaLineView.isHidden = true
            self.areaBtn.setTitle("", for: .normal)
            isArea = false
            tempSelectAreaCount = ""
            tempSelectCityCount = ""
            self.cityTableView.reloadData()
        } else {
            if tempSelectCityCount.count <= 0 {
                Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_select_city))
                return
            }
            if tempSelectAreaCount.count <= 0 {
                Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_select_city))
                return
            }
            //如果为选中的时候点击X号则不标记为选中状态
            //如果点击确定的时候，是到城市级别就 标记为选中状态
            if !isArea {
                areaAry = nil
                tempSelectCityCount = ""
                tempSelectAreaCount = ""
                tempIsSelect = false
            } else {
                tempIsSelect = true
                selectCityCount = tempSelectCityCount
                selectAreaCount = tempSelectAreaCount
                isSelect = tempIsSelect
            }
            self.delegate?.selectCityConfirmDelegateIsSender(isSender: isSender)
        }
    }
    
    @objc func clickAreaOrCityBtn(sender:UIButton) {
        if sender.tag == 110 {
            self.cityLineView.isHidden = false
            self.areaLineView.isHidden = true
            //城市不变 区域变回去 且清空区域选择
            self.cityBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
            self.areaBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
            tempSelectAreaCount = ""
            isArea = false
            self.cityTableView.reloadData()
        } else if sender.tag == 111 {
            self.cityLineView.isHidden = true
            self.areaLineView.isHidden = false
            self.areaBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
            self.cityBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.endEditing(true)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isArea {
            return areaAry.count
        } else {
            return cityAry.count
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    /*
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if (isArea) {
            let dicData = areaAry[indexPath.row]
            tempSelectAreaCount = String(indexPath.row)
            self.areaBtn.setTitle(dicData["name"], for: .normal)
            areaBtn.snp.updateConstraints { (make) in
                let width =  dicData["name"].getWidth(UIFont.boldSystemFont(ofSize: 16))
                make.width.equalTo(width + 5)
            }
            self.cityTableView.reloadData()
        } else {
            let dicData = cityAry[indexPath.row]
            tempSelectCityCount = String(indexPath.row)
            self.cityBtn.setTitle(dicData["name"], for: .normal)
            self.cityBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
            self.areaBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.select_ALL_Area), for: .normal)
            self.areaBtn.snp.updateConstraints { (make) in
                let width =  ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.select_ALL_Area).getWidth(UIFont.boldSystemFont(ofSize: 16))
                make.width.equalTo(width + 5)
            }
            self.cityBtn.snp.makeConstraints { (make) in
                let width =  dicData["name"].getWidth(UIFont.boldSystemFont(ofSize: 16))
                make.width.equalTo(width + 5)
            }
            self.areaBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
            self.cityLineView.isHidden = true
            self.areaLineView.isHidden = false
            //获取城市
            let dic:Dictionary = ["opreateType":isSender,"orderStatus":orderStatus,"cityId":dicData["cityId"]]
            
            Constants.window?.makeToast("")
            HttpService.sharedHttpService().getScreenArea(mutDic: dic) { (json, state, error) in
                Constants.window?.hideAllToasts()
                let ary = json["date"].array
                let dic = ["name":ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_order_All,"regionId":"","regionIdCount":"")]
                
                

            }

            
        }
    }
 */
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:OrderSeleectCityCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! OrderSeleectCityCell
        if isArea {
            
        }
        
        return cell
    }
    
    lazy var mainView: UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.masksToBounds = true
        mainView.layer.cornerRadius = 15
        return mainView
    }()
    
    lazy var cencelBtn: UIButton = {
        let cencelBtn = UIButton.init(type: .custom)
        cencelBtn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        cencelBtn.addTarget(self, action: #selector(clickCancelBtn), for: .touchUpInside)
        return cencelBtn
    }()
    
    lazy var titleLab: UILabel = {
        let titleLab = UILabel.init()
        titleLab.font = UIFont.systemFont(ofSize: 16)
        titleLab.textColor = UIColor.init(hexString: "#2B2B2B")
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.init(hexString: "#EDEDED")
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(self.mainView.snp.top).offset(50)
            make.left.equalTo(self.snp.left).offset(0)
            make.right.equalTo(self.snp.right).offset(0)
            make.height.equalTo(1)
        }
        self.addSubview(self.cityBtn)
        self.addSubview(self.areaBtn)
        cityBtn.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(7.5)
            make.left.equalTo(self.snp.left).offset(24)
            make.height.equalTo(22.5)
            make.width.equalTo(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect).getWidth(UIFont.boldSystemFont(ofSize: 16)) + 5)
        }
        self.cityBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect), for: .normal)
        self.cityBtn.titleLabel?.textColor = UIColor.init(hexString: "#FF7908")
        self.areaBtn.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(7.5)
            make.left.equalTo(cityBtn.snp.right).offset(60)
            make.height.equalTo(22.5)
            make.height.equalTo(70)
        }
        let lineTwoView = UIView()
        lineTwoView.backgroundColor = UIColor.init(hexString: "#EDEDED")
        self.addSubview(lineTwoView)
        lineTwoView.snp.makeConstraints { (make) in
            make.top.equalTo(cityBtn.snp.bottom).offset(7)
            make.left.equalTo(cityBtn.snp.left).offset(0)
            make.right.equalTo(cityBtn.snp.right).offset(0)
            make.height.equalTo(1)
        }
        self.addSubview(self.cityLineView)
        cityLineView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(36.5)
            make.height.equalTo(1.5)
            make.left.equalTo(cityBtn.snp.left).offset(5)
            make.right.equalTo(cityBtn.snp.right).offset(-5)
        }
        self.addSubview(self.areaLineView)
        areaLineView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(36.5)
            make.height.equalTo(1.5)
            make.left.equalTo(self.areaBtn.snp.left).offset(5)
            make.right.equalTo(self.areaBtn.snp.right).offset(-5)
        }
        cityLineView.isHidden = false
        areaLineView.isHidden = true
        return titleLab
    }()
    
    lazy var cityTableView: UITableView = {
        let cityTableView = UITableView.init()
        cityTableView.delegate = self
        cityTableView.dataSource = self
        cityTableView.register(UINib.init(nibName: "OrderSeleectCityCell", bundle: nil), forCellReuseIdentifier: "cell")
        cityTableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        return cityTableView
    }()
    
    lazy var cityBtn: UIButton = {
        let cityBtn = UIButton.init(type: .custom)
        cityBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
        cityBtn.addTarget(self, action: #selector(clickAreaOrCityBtn), for: .touchUpInside)
        cityBtn.tag = 110
        cityBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        return cityBtn
    }()
    
    lazy var areaBtn: UIButton = {
        let areaBtn = UIButton.init(type: .custom)
        areaBtn.setTitleColor(UIColor.init(hexString: "#FF7908"), for: .normal)
        areaBtn.addTarget(self, action: #selector(clickAreaOrCityBtn), for: .touchUpInside)
        areaBtn.tag = 111
        areaBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        return areaBtn
    }()
    
    lazy var resetBtn: UIButton = {
        let resetBtn = UIButton.init(type: .custom)
        resetBtn.setTitleColor(UIColor.init(hexString: "#FFFFFF"), for: .normal)
        resetBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_msg_reset), for: .normal)
        resetBtn.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
        resetBtn.tag = 100
        resetBtn.layer.cornerRadius = 20
        resetBtn.backgroundColor = UIColor.init(hexString: "#D8D8D8")
        resetBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        return resetBtn
    }()
    
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        confirmBtn.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
        confirmBtn.tag = 101
        confirmBtn.layer.cornerRadius = 20
        confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        confirmBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        return confirmBtn
    }()
    
    lazy var cityLineView: UIView = {
        let cityLineView = UIView()
        cityLineView.backgroundColor = UIColor.init(hexString: "#FF6E0A")
        return cityLineView
    }()
    
    lazy var areaLineView: UIView = {
        let areaLineView = UIView()
        areaLineView.backgroundColor = UIColor.init(hexString: "#FF6E0A")
        return areaLineView
    }()
}
